#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/input.h>
#include <sys/mman.h>
#include <stdlib.h>
#include "ui.h"
int main()
{
    int rfid_rightid = 0x76308b3a;
    int rfid_readid;
    int bmp_x,bmp_y;//lcd坐标变量
    int videoapp=0;
    int musicapp=0,music_mod=0,music_photo = 0,music_list = 0;//app：程序启动与否标志位 mod：播放暂停 photo：封面选择
    int cache[800*480];//显示缓存
    int *tmp;
    int lcd = open("/dev/fb0",O_RDWR);   

    int touch_fd = open("/dev/input/event0",O_RDONLY);
    int touch_x,touch_y;
    struct input_event touch;
    if(lcd == -1)
    {
        printf("打开失败\n");
        return -1;
    }
    int page = 0;
    int *argb = mmap(NULL,800*480*4,PROT_READ|PROT_WRITE,MAP_SHARED,lcd,0);
    int bmp_n=0;
    while(1)
    {
        switch (page)
        {
        case 0:tmp = ui_lock(cache);printf("0");
            break;
        case 1:tmp = ui_index(cache);printf("1");
            break;    
        case 2:ui_photo(cache);printf("2");
            break; 
        case 3:tmp = ui_music(cache,music_mod,music_photo,music_list);printf("3");
            break;
        case 4:tmp = ui_video(cache,0);printf("4");
            break;   
        default:
            break;
        }
        for(bmp_n=0;bmp_n<800*480;bmp_n++)
        {
            *(argb+bmp_n) = tmp[bmp_n];
        }//写入显存
        //////
        if(rfid_readid == rfid_rightid )
        {
            page = 1; 
        }
        //////
        //
        read(touch_fd,&touch,sizeof(touch));
        if(touch.type == EV_ABS && touch.code == ABS_X )
            touch_x=touch.value*800/1024;
        if(touch.type == EV_ABS && touch.code == ABS_Y )
            touch_y=touch.value*480/600;
        if(touch.code == BTN_TOUCH && touch.value == 0 && touch.type == EV_KEY)
            printf("x=%d,y=%d\n",touch_x,touch_y);
        //读取触摸坐标
        if(page == 0 || page == 1 )
        {
            if(touch.code == BTN_TOUCH && touch.value == 0 && touch.type == EV_KEY)
            {
                if(touch_x>=700 && touch_y<=100)
                {
                    printf("x=%d,y=%d,exit\n",touch_x,touch_y);
                    for(bmp_n=0;bmp_n<800*480;bmp_n++)
                    {
                        *(argb+bmp_n) = 0x00000000;
                    }
                    break;
                }
            }
        }//上锁界面以及首页的退出
        if(page == 0)//解锁页面
        {
            if(touch.code == BTN_TOUCH && touch.value == 0 && touch.type == EV_KEY)
            {
                if(touch_x<=500 && touch_x>=300 && touch_y<=360 && touch_y>=120)
                {
                    printf("x=%d,y=%d,goindex\n",touch_x,touch_y);
                    page = 1;//解锁->跳转首页
                    continue;
                }
            }            
        }
        
        if(page == 1)//主页面
        {
            if(touch.code == BTN_TOUCH && touch.value == 0 && touch.type == EV_KEY)
            {
                if(touch_x>=700 && touch_y<=100)
                {
                    printf("x=%d,y=%d,exit\n",touch_x,touch_y);
                    break;
                }//退出
                if(touch_x>=100 &&touch_x<200 &&touch_y>100 && touch_y<200)
                {
                    printf("x=%d,y=%d,video\n",touch_x,touch_y);
                    page = 4;
                    continue;
                    //视频
                }
                if(touch_x>=100 &&touch_x<200 &&touch_y>220 && touch_y<400)
                {
                    printf("x=%d,y=%d,photo\n",touch_x,touch_y);
                    system("../photo");
                    continue;                   
                    //相册
                }
                if(touch_x>=360 &&touch_x<480 &&touch_y>100 && touch_y<200)
                {
                    printf("x=%d,y=%d,music\n",touch_x,touch_y);
                    //page = 3;
                    system("./music");
                    continue;
                    //音乐
                }
                if(touch_x>=360 &&touch_x<480 &&touch_y>220 && touch_y<400)
                {
                    printf("x=%d,y=%d,game\n",touch_x,touch_y);
                    break;
                    //无畏契约
                }                
            }
        }
        if(page == 2)//相册
        {
            printf("无");break;
            //调用相册程序
        }
        // if(page == 3)//MUSIC
        // {
        //     if(touch.code == BTN_TOUCH && touch.value == 0 && touch.type == EV_KEY)
        //     {
        //         if(touch_x>=700 && touch_x <800 &&touch_y>=380)
        //         {
        //             printf("x=%d,y=%d,exit\n",touch_x,touch_y);
        //             page = 1;//退出跳转首页
        //             continue;
        //         }
        //         if(touch_x>0 && touch_x <100 && touch_y>=380 && touch_y<480)
        //         {
        //             printf("x=%d,y=%d,歌单\n",touch_x,touch_y);
        //             music_list++;music_list%=2;
        //         }//显示歌单
        //         if(touch_x>=200 && touch_x<=300 && touch_y>=380 && touch_y<480)
        //         {
        //             printf("x=%d,y=%d,上一首\n",touch_x,touch_y);
        //         }//上一首
        //         if(touch_x>=350 && touch_x<=450 && touch_y>=380 && touch_y<480)
        //         {
        //             printf("x=%d,y=%d,暂停播放\n",touch_x,touch_y);
        //             if(musicapp == 0)
        //             {
                        
        //             }
        //         }//暂停或播放
        //         if(touch_x>=510 && touch_x<=600 && touch_y>=380 && touch_y<480)
        //         {
        //             printf("x=%d,y=%d,下一首\n",touch_x,touch_y);
        //         }//下一首
        //     }
        // }
        if(page == 4)
        {
            if(touch.code == BTN_TOUCH && touch.value == 0 && touch.type == EV_KEY)
            {
                if(touch_x>=700 && touch_x <800 &&touch_y>=380)
                {
                    printf("x=%d,y=%d,exit\n",touch_x,touch_y);
                    page = 1;//退出跳转首页
                    continue;
                }
                if(touch_x>=130 && touch_x<=230 && touch_y>=380 && touch_y<480)
                {
                    printf("x=%d,y=%d,exit\n",touch_x,touch_y);
                }//快退
                if(touch_x>=320 && touch_x<=420 && touch_y>=380 && touch_y<480)
                {
                    printf("x=%d,y=%d,exit\n",touch_x,touch_y);
                }//暂停或播放
                if(touch_x>=520 && touch_x<=620 && touch_y>=380 && touch_y<480)
                {
                    printf("x=%d,y=%d,exit\n",touch_x,touch_y);
                }//快进
            }           
            //video
        }
        if(page == 5)
        {
            printf("无");break;
            //疑似其他
        }
    }
    //tmp = ui_music(cache,0,0);
    // for(bmp_n=0;bmp_n<800*480;bmp_n++)
    // {
    //     *(argb+bmp_n) = tmp[bmp_n];
    // }
    printf("结束");
    munmap(argb,480*800*4);
    close(lcd);
    close(touch_fd);
    return 0;
}
